<?php
class CommonFunc {
	
	public static function subAddress($addr, $num = 48) {
		$len = strlen ( $addr );
		$suffix = '';
		
		if ($len > $num) {
			$len = $num;
			$suffix = '...';
			for($idx = 0; $idx < $len;) {
				if (ord ( $addr [$idx] ) > 0x7f) {
					$idx += 3;
				} else {
					$idx ++;
				}
			}
			return substr ( $addr, 0, $idx ) . $suffix;
		} else
			return $addr;
	
	}
	
	public static function formatUnixtime($timestamp, $type) {
		
		switch ($type) {
			case 1 :
				return date ( 'Y-m-d H:i:s', $timestamp );
				break;
			case 2 :
				return date ( 'm-d', $timestamp );
				break;
			case 3 :
				return date ( 'm/d', $timestamp );
				break;
		}
	}
	
  public static function  Html2Text($str, $r = 0) {
		if (! function_exists ( 'SpHtml2Text' )) {
			function SpHtml2Text($str) {
				$str = preg_replace ( "/<sty(.*)\\/style>|<scr(.*)\\/script>|<!--(.*)-->/isU", "", $str );
				$alltext = "";
				$start = 1;
				for($i = 0; $i < strlen ( $str ); $i ++) {
					if ($start == 0 && $str [$i] == ">") {
						$start = 1;
					} else if ($start == 1) {
						if ($str [$i] == "<") {
							$start = 0;
							$alltext .= " ";
						} else if (ord ( $str [$i] ) > 31) {
							$alltext .= $str [$i];
						}
					}
				}
				$alltext = str_replace ( "　", " ", $alltext );
				$alltext = preg_replace ( "/&([^;&]*)(;|&)/", "", $alltext );
				$alltext = preg_replace ( "/[ ]+/s", " ", $alltext );
				return $alltext;
			}
		}
		if ($r == 0) {
			return SpHtml2Text ( $str );
		} else {
			$str = SpHtml2Text ( stripslashes ( $str ) );
			return addslashes ( $str );
		}
	}
	
	
	
}